package cloud.keveri.springboot.starter.gateway.config;

import cloud.keveri.gateway.config.api.ConfigCenterService;
import cloud.keveri.gateway.config.api.listener.RulesChangeSubscriber;
import cloud.keveri.gateway.config.nacos.NacosConfigCenterService;
import cloud.keveri.gateway.config.nacos.configure.NacosConfig;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Properties;

import static cloud.keveri.gateway.config.api.def.Const.Gateway_Config_Prefix;

/**
 * <p>
 * 作者：沈自在
 **/
@Slf4j
@Configuration
@ConditionalOnClass(NacosConfigCenterService.class)
@ConditionalOnProperty(prefix = Gateway_Config_Prefix, name = "url")
public class GatewayConfigAutoConfiguration {

    /**
     *
     */
    @Bean
    public ConfigCenterService nacosConfigCenterService(final ObjectProvider<ConfigService> configService,
                                                        final ObjectProvider<List<RulesChangeSubscriber>> ruleSubscribers,
                                                        final NacosConfig nacosConfig) {
        log.info("[Keveri-Cloud] 将由 Nacos 接管配置同步于刷新");
        return new NacosConfigCenterService(configService.getIfAvailable(), nacosConfig.getGroup(), ruleSubscribers.getIfAvailable());
    }

    /**
     *
     */
    @Bean
    public ConfigService nacosConfigService(final NacosConfig nacosConfig) throws Exception {
        Properties properties = new Properties();
        if (nacosConfig.getAcm() != null && nacosConfig.getAcm().isEnabled()) {
            properties.put(PropertyKeyConst.ENDPOINT, nacosConfig.getAcm().getEndpoint());
            properties.put(PropertyKeyConst.NAMESPACE, nacosConfig.getAcm().getNamespace());
            properties.put(PropertyKeyConst.ACCESS_KEY, nacosConfig.getAcm().getAccessKey());
            properties.put(PropertyKeyConst.SECRET_KEY, nacosConfig.getAcm().getSecretKey());
        } else {
            properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfig.getUrl());
            if (StringUtils.isNotBlank(nacosConfig.getNamespace())) {
                properties.put(PropertyKeyConst.NAMESPACE, nacosConfig.getNamespace());
            }
            if (nacosConfig.getUsername() != null) {
                properties.put(PropertyKeyConst.USERNAME, nacosConfig.getUsername());
            }
            if (nacosConfig.getPassword() != null) {
                properties.put(PropertyKeyConst.PASSWORD, nacosConfig.getPassword());
            }
        }
        return NacosFactory.createConfigService(properties);
    }

    /**
     *
     */
    @Bean
    @ConfigurationProperties(prefix = Gateway_Config_Prefix)
    public NacosConfig nacosConfig() {
        return new NacosConfig();
    }
}
